package com.cloxtec.scada.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author iot.cloxtec.com
 * @date 2018年7月31日
 */
public class ResultUtil {
    private static final String CODE = "code";
    private static final String MESSAGE = "msg";

    public static JSONObject successJSONObject(Object bean) {
        return successJSONObject(bean.getClass().getSimpleName(), bean);
    }

    public static JSONObject successJSONObject(String key, Object obj) {
        JSONObject result = genJSONObject(Status.OK);
        result.put(key, JSON.toJSON(obj));
        return result;
    }


    public static JSONObject errorJSONObject(Status status) {
        return genJSONObject(status);
    }

    public static JSONObject errorJSONObject(String code, String message) {
        return genJSONObject(code, message);
    }

    public static String successJSONString(Object bean) {
        return successJSONObject(bean).toJSONString();
    }

    public static String errorJSONString(Status status) {
        return errorJSONObject(status).toJSONString();
    }

    public static String errorJSONString(String code, String message) {
        return errorJSONObject(code, message).toJSONString();
    }

    public static JSONObject genJSONObject(Status status) {
        return genJSONObject(status.getCode(), status.getMessage());
    }

    public static JSONObject getSortJsonObject(Status status) {
        return genSortJsonObject(status.getCode(), status.getMessage());
    }

    public static JSONObject genSortJsonObject(String code, String message) {
        JSONObject result = new JSONObject(true);
        JSONObject jo = new JSONObject();
        jo.put(CODE, code);
        jo.put(MESSAGE, message);
        result.put("status", jo);
        return result;
    }


    public static JSONObject genJSONObject(String code, String message) {
        JSONObject result = new JSONObject();
        JSONObject jo = new JSONObject();
        jo.put(CODE, code);
        jo.put(MESSAGE, message);
        result.put("status", jo);
        return result;
    }
}
